package com.example.shellshopping.personCenter;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.shellshopping.R;
import com.example.shellshopping.login.UserBean;
import com.example.shellshopping.utils.BaseActivity;
import com.example.shellshopping.utils.SaveUserUtils;
import com.example.shellshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PersonSettingRepwdActivity extends BaseActivity {

    EditText pwd_et,repwd_et;
    Button next_bt;
    ImageView backIV;

    private UserBean.DataBean userInfo; //之前登录时保存的用户信息
    UserBean.DataBean newUserInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_setting_repwd);
        userInfo = SaveUserUtils.getUserInfo(this);
        newUserInfo = new UserBean.DataBean(userInfo.getId(),userInfo.getPhone(),userInfo.getPassword(),
                userInfo.getName(),userInfo.getAddress(),userInfo.getNickname(),userInfo.getQuestion(),
                userInfo.getAnswer());

        initView();
        setListener();

    }

    private void setListener() {
        next_bt.setOnClickListener(onClickListener);
        backIV.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.person_setting_repwd_btn:
                    //修改密码
                    isAnswer();
                    break;
                case R.id.person_setting_repwd_iv_back:
                    //修改密码
                    jumpToQuestion();
                    break;
            }
        }
    };

    private void isAnswer() {
        String pwd = pwd_et.getText().toString();
        String repwd = repwd_et.getText().toString();
        newUserInfo.setPassword(pwd);
        boolean flag = judgeInput(pwd,repwd);  //判断输入是否合规
        if (flag) {  //可以执行网络请求
            Map<String,String> map = new HashMap<>();
            map.put("userId", String.valueOf(userInfo.getId()));
            map.put("oldPwd",userInfo.getPassword());
            map.put("newPwd",pwd);
            postNetwork(URLUtils.updatePwd_url,map);
        }
    }

    private boolean judgeInput(String pwd,String repwd) {
        //判断两次输入是否为空
        if (TextUtils.isEmpty(pwd)||TextUtils.isEmpty(repwd)) {
            Toast.makeText(this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }

        //判断两次输入是否相同
        if (!pwd.equals(repwd)) {
            Toast.makeText(this,"两次输入密码不相同",Toast.LENGTH_SHORT).show();
            return false;
        }

        //判断旧密码是否与新密码相同
        if (pwd.equals(userInfo.getPassword())) {
            Toast.makeText(this,"与原密码相同",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    public void onSuccess(String result){
        super.onSuccess(result);
        //解析数据
        UserBean bean = new Gson().fromJson(result,UserBean.class);
        //
        int code = bean.getCode();
        switch(code){
            case 200:  //修改成功
                SaveUserUtils.saveUserToFile(this,newUserInfo);
                userInfo = SaveUserUtils.getUserInfo(this);
                //jumpToSetting();
                //showBackDialog();
                finish();
                jumpToSetting();
                break;
            case 201:  //账号或密码错误
                break;
            default:   //其他，登录失败
                break;
        }
    }

    //跳到设置界面
    private void jumpToSetting() {
        finish();
        Intent intent = new Intent(this, PersonCenterSettingActivity.class);
        startActivity(intent);
    }

    //跳到密保界面
    private void jumpToQuestion() {
        finish();
        Intent intent = new Intent(this, PersonSettingQuestionActivity.class);
        startActivity(intent);
    }

    //弹出返回对话框
    private void showBackDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("页面提示");
        builder.setMessage("密码已修改成功\n请单击确认返回设置界面");
        builder.setIcon(R.mipmap.icon);
        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
                jumpToSetting();
            }
        });
        builder.create().show();
    }


    private void initView() {
        pwd_et = findViewById(R.id.person_setting_repwd_et1);
        repwd_et = findViewById(R.id.person_setting_repwd_et2);
        next_bt = findViewById(R.id.person_setting_repwd_btn);
        backIV = findViewById(R.id.person_setting_repwd_iv_back);
    }
}